//get ajax
function AJAX_GET(options) {
  let xhr = new XMLHttpRequest()
  if (!options.url) {
    alert('地址不能为空')
    return
  }
  xhr.onreadystatechange = () => {
    if (xhr.readyState === 4) {
      if (xhr.status >= 200 && xhr.status < 300) {
        options.success && options.success(xhr.responseText)
      } else {
        options.error && options.error()
      }
    }
  }
  if (options.query) {
    let str = '?'
    for (let k in options.query) {
      str += `${k}=${options.query[k]}`
      str += '&'
    }
    str = str.slice(0, -1)
    xhr.open("GET", options.url + str)
  } else {
    xhr.open("GET", options.url)
  }
  xhr.send()
}


//promise
function P_AJAX(options) {
  return new Promise((resolve,reject) => {
    let xhr = new XMLHttpRequest()
    if (!options.url) {
      alert('地址不能为空')
      return
    }
    xhr.onreadystatechange = () => {
      if (xhr.readyState === 4) {
        if (xhr.status >= 200 && xhr.status < 300) {
          resolve(xhr.responseText)
        } else {
          reject()
        }
      }
    }
    if (options.query) {
      let str = '?'
      for (let k in options.query) {
        str += `${k}=${options.query[k]}`
        str += '&'
      }
      str = str.slice(0, -1)
      xhr.open("GET", options.url + str)
    } else {
      xhr.open("GET", options.url)
    }
    xhr.send()
  })
}